package com.cy.ppj.service.mall.impl;

import com.cy.ppj.comm.constant.Constants;
import com.cy.ppj.dao.mapper.mall.GoodsTypeMapper;
import com.cy.ppj.model.PageBean;
import com.cy.ppj.model.domian.mall.GoodsType;
import com.cy.ppj.model.enums.CommonStatus;
import com.cy.ppj.model.query.mall.GoodsTypeAdminListQuery;
import com.cy.ppj.model.vo.mall.GoodsTypeAdminListVO;
import com.cy.ppj.service.BaseService;
import com.cy.ppj.service.comm.FileService;
import com.cy.ppj.service.mall.GoodsTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @Description 商品品类 接口实现类
 * @Author tanyilin
 * @Date 2019/9/27 14:48
 */
@Service
public class GoodsTypeServiceImpl extends BaseService implements GoodsTypeService {

    @Autowired
    private GoodsTypeMapper goodsTypeMapper;
    @Autowired
    private FileService fileService;

    @Override
    public PageBean<GoodsTypeAdminListVO> getGoodsTypeAdminList(GoodsTypeAdminListQuery query) {

        int total = goodsTypeMapper.selectGoodsTypeAdminListCount(query);
        List<GoodsTypeAdminListVO> list = new ArrayList<>();
        if(total > 0) {
            query.startPage();
            list = goodsTypeMapper.selectGoodsTypeAdminList(query);
            list.stream().forEach(gt -> {
                gt.setStatusText(gt.getStatus().getText2());
                gt.setLogoSrc(fileService.getFileUrl(gt.getLogo(), Constants.MALL_BUCKET_NAME));
            });
        }

        return new PageBean<>(list, total);
    }

    @Override
    @Transactional
    public void addGoodsType(GoodsType goodsType) {

        Date time = new Date();
        goodsType.setWeight(goodsTypeMapper.selectMaxWeight() + 1);
        goodsType.setStatus(CommonStatus.ENABLE);
        goodsType.setCreateTime(time);
        goodsType.setUpdateTime(time);
        goodsTypeMapper.insertSelective(goodsType);

    }

    @Override
    @Transactional
    public void updateGoodsType(GoodsType goodsType) {

        Date time = new Date();
        goodsType.setUpdateTime(time);
        goodsTypeMapper.updateByPrimaryKeySelective(goodsType);

    }

    @Override
    public List<GoodsType> getAllGoodsTypeList() {

        List<GoodsType> list = goodsTypeMapper.selectAllGoodsTypeList();
        return list;
    }


}
